package com.web.cms.doc.util;

import java.util.ArrayList;
import java.util.List;

import com.blue.core.util.NumberUtil;
import com.blue.jdbc.Page;
import com.web.cms.doc.model.Web;
import com.web.cms.doc.message.WebRequest;
import com.web.cms.doc.message.WebResponse;

public class WebConvertor
{
	private WebConvertor()
	{
	}
	
	public static Web toWeb(WebRequest request)
	{
		if (request == null)
			return null;
		
		Web web = new Web();
		web.setId(request.getId());
		web.setCode(request.getCode());
		web.setName(request.getName());
		web.setDomain(request.getDomain());
		web.setDescp(request.getDescp());
		web.setCreateTime(request.getCreateTime());
		web.setState(request.getState());
		web.setChannels(request.getChannels());
		web.setDocs(request.getDocs());
		web.setFiles(request.getFiles());
		web.setFileSize(request.getFileSize());
		return web;
	}
	
	public static WebResponse toResponse(Web web)
	{
		if (web == null)
			return null;
		
		WebResponse response = new WebResponse();
		response.setId(web.getId());
		response.setCode(web.getCode());
		response.setName(web.getName());
		response.setDomain(web.getDomain());
		response.setDescp(web.getDescp());
		response.setCreateTime(web.getCreateTime());
		response.setState(web.getState());
		response.setChannels(web.getChannels());
		response.setDocs(web.getDocs());
		response.setFiles(web.getFiles());
		response.setFileSize(web.getFileSize());
		
		response.setFileSizeName(NumberUtil.byteFormat(response.getFileSize().longValue()));
		return response;
	}
	
	public static List<WebResponse> toResponse(List<Web> list)
	{
		List<WebResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (Web web : list)
		{
			responseList.add(toResponse(web));
		}
		return responseList;
	}
	
	public static Page<WebResponse> toResponse(Page<Web> page)
	{
		Page<WebResponse> pageResponse = new Page<>();
		pageResponse.setCurPage(page.getCurPage());
		pageResponse.setRows(page.getRows());
		pageResponse.setSize(page.getSize());
		pageResponse.setStartRowNo(page.getStartRowNo());
		pageResponse.setTotalPage(page.getTotalPage());
		pageResponse.setObjectList(toResponse(page.getObjectList()));
		return pageResponse;
	}
	
}
